package com.itheima.bos.web.action.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.itheima.bos.domain.base.FixedArea;
import com.itheima.bos.service.base.FixedAreaService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class FixedAreaAction extends ActionSupport implements ModelDriven<FixedArea>{

	//模型驱动
	private FixedArea fixedArea=new FixedArea();
	
	@Override
	public FixedArea getModel() {
		// TODO Auto-generated method stub
		return fixedArea;
	}
	
	//注入service
	@Autowired
	private FixedAreaService fixedAreaService;
	
	@Action(value="fixedArea_save",results={@Result(name="success",
			type="redirect",location="./pages/base/fixed_area.html")
	})
	public String save(){
		System.out.println("你好 哈哈哈哈"+fixedArea.getFixedAreaName());
		//调用业务层,保存定区
		fixedAreaService.save(fixedArea);
		return SUCCESS;
	}
	
	//属性驱动接受客户端分页参数
	private int page;
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	
	@Action(value="fixedArea_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		//构造分页查询对象
		Pageable pageable=new PageRequest(page-1, rows);
		//构造条件查询对象
		Specification<FixedArea> specification=new Specification<FixedArea>() {

			@Override
			public Predicate toPredicate(Root<FixedArea> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list=new ArrayList<Predicate>();
				//构造查询条件
				if(StringUtils.isNotBlank(fixedArea.getId())){
					//根据定区编号查询等值
					Predicate p1 = cb.equal(root.get("id").as(String.class),fixedArea.getId());
					list.add(p1);
				}
				if(StringUtils.isNotBlank(fixedArea.getCompany())){
					//根据公司模糊查询
					Predicate p2 = cb.like(root.get("company").as(String.class), "%"+fixedArea.getCompany()+"%");
					list.add(p2);
				}
				
				
				return cb.and(list.toArray(new Predicate[0])); 
			}
		};
		
		//调用业务层
		Page<FixedArea> pageData= fixedAreaService.findPageData(specification, pageable);
		
		Map<String, Object> result=new HashMap<String, Object>();
		result.put("total", pageData.getTotalElements());
		result.put("rows", pageData.getContent());
		//将查询结果压入值栈顶部转换json
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
		
	}

}
